package com.isxcode.demo;

import com.isxcode.demo.pojo.ConnectInfo;
import com.isxcode.demo.pojo.DorisReqDto;
import com.isxcode.demo.service.DorisService;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
@SpringBootApplication
public class DemoApplication {

	private final DorisService dorisService;

	public DemoApplication(DorisService dorisService) {
		this.dorisService = dorisService;
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@GetMapping("/demo")
	public String demo() {

		return "hello world";
	}

	@PostMapping("/demo1")
	public Boolean demo1(@RequestBody DorisReqDto dorisReqDto) {

		ConnectInfo connectInfo = new ConnectInfo();
		BeanUtils.copyProperties(dorisReqDto, connectInfo);
		return dorisService.checkDorisLink(connectInfo);
	}

	@PostMapping("/demo2")
	public List<String> demo2(@RequestBody DorisReqDto dorisReqDto) throws Exception {

		ConnectInfo connectInfo = new ConnectInfo();
		BeanUtils.copyProperties(dorisReqDto, connectInfo);
		return dorisService.queryDatabaseNames(connectInfo);
	}

	@PostMapping("/demo3")
	public List<String> demo3(@RequestBody DorisReqDto dorisReqDto) throws Exception {

		ConnectInfo connectInfo = new ConnectInfo();
		BeanUtils.copyProperties(dorisReqDto, connectInfo);
		return dorisService.queryTableNames(connectInfo);
	}

	@PostMapping("/demo4")
	public List<List<String>> demo4(@RequestBody DorisReqDto dorisReqDto) throws Exception {

		ConnectInfo connectInfo = new ConnectInfo();
		BeanUtils.copyProperties(dorisReqDto, connectInfo);
		return dorisService.queryData(connectInfo, dorisReqDto.getTableName());
	}

	@PostMapping("/demo5")
	public List<List<String>> demo5(@RequestBody DorisReqDto dorisReqDto) throws Exception {

		ConnectInfo connectInfo = new ConnectInfo();
		BeanUtils.copyProperties(dorisReqDto, connectInfo);
		return dorisService.queryColumnNames(connectInfo, dorisReqDto.getTableName());
	}

	@PostMapping("/demo6")
	public void demo6(@RequestBody DorisReqDto dorisReqDto) throws Exception {

		ConnectInfo connectInfo = new ConnectInfo();
		BeanUtils.copyProperties(dorisReqDto, connectInfo);
		dorisService.executeDorisSql(connectInfo, dorisReqDto.getSql());
	}

}
